package com.ruoyi.control.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.dto.BeltDto;
import com.ruoyi.control.dto.ElectricityHistoryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.ElectricityHistoryMapper;
import com.ruoyi.control.domain.ElectricityHistory;
import com.ruoyi.control.service.IElectricityHistoryService;

/**
 * 电力历史Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-09
 */
@Service
public class ElectricityHistoryServiceImpl extends ServiceImpl<ElectricityHistoryMapper, ElectricityHistory> implements IElectricityHistoryService //修改
{
    @Autowired
    private ElectricityHistoryMapper electricityHistoryMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询电力历史
     *
     * @param id 电力历史主键
     * @return 电力历史
     */
    @Override
    public ElectricityHistory selectElectricityHistoryById(String id) {
        return electricityHistoryMapper.selectElectricityHistoryById(id);
    }

    /**
     * 查询电力历史列表
     *
     * @param electricityHistory 电力历史
     * @return 电力历史
     */
    @Override
    public List<ElectricityHistory> selectElectricityHistoryList(ElectricityHistory electricityHistory) {
        return electricityHistoryMapper.selectElectricityHistoryList(electricityHistory);
    }

    /**
     * 新增电力历史
     *
     * @param electricityHistory 电力历史
     * @return 结果
     */
    @Override
    public int insertElectricityHistory(ElectricityHistory electricityHistory) {
        return electricityHistoryMapper.insertElectricityHistory(electricityHistory);
    }

    /**
     * 修改电力历史
     *
     * @param electricityHistory 电力历史
     * @return 结果
     */
    @Override
    public int updateElectricityHistory(ElectricityHistory electricityHistory) {
        electricityHistory.setUpdateTime(DateUtils.getNowDate());
        return electricityHistoryMapper.updateElectricityHistory(electricityHistory);
    }

    /**
     * 批量删除电力历史
     *
     * @param ids 需要删除的电力历史主键
     * @return 结果
     */
    @Override
    public int deleteElectricityHistoryByIds(String[] ids) {
        return electricityHistoryMapper.deleteElectricityHistoryByIds(ids);
    }

    /**
     * 删除电力历史信息
     *
     * @param id 电力历史主键
     * @return 结果
     */
    @Override
    public int deleteElectricityHistoryById(String id) {
        return electricityHistoryMapper.deleteElectricityHistoryById(id);
    }

    @Override
    public int getElectricTotal() {
        final String key = "gdKey";
        int total = 0;
//        String cachedValue = redisCache.getCacheObject(key);

        List<ElectricityHistoryDto> list = electricityHistoryMapper.getElectricTotal();
        total = list.size();
        redisCache.setCacheObject(key, String.valueOf(total));

        String gdKey1 = redisCache.getCacheObject("gdKey");
        Integer cachedValue = Integer.valueOf((gdKey1 == null ? "0" : gdKey1));

//        if (cachedValue == null) {
//            List<ElectricityHistoryDto> list = electricityHistoryMapper.getElectricTotal();
//            total = list.size();
//            redisCache.setCacheObject(key, String.valueOf(total));
//        } else {
//            total = Integer.parseInt(cachedValue);
//            System.out.println("读取gdKey缓存: " + total);
//        }
        return cachedValue;
    }

}
